﻿
// STLViewerView.cpp: CSTLViewerView 类的实现
//

#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "STLViewer.h"
#endif

#include "STLViewerDoc.h"
#include "STLViewerView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


using namespace stl;

// CSTLViewerView

IMPLEMENT_DYNCREATE(CSTLViewerView, CGLView)

BEGIN_MESSAGE_MAP(CSTLViewerView, CGLView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CGLView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CGLView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CSTLViewerView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()

	ON_COMMAND(ID_STL_VIEW_BACK, OnViewBack)
	ON_COMMAND(ID_STL_VIEW_BOTTOM, OnViewBottom)
	ON_COMMAND(ID_STL_VIEW_FRONT, OnViewFront)
	ON_COMMAND(ID_STL_VIEW_LEFT, OnViewLeft)
	ON_COMMAND(ID_STL_VIEW_RIGHT, OnViewRight)
	ON_COMMAND(ID_STL_VIEW_TOP, OnViewTop)
	ON_COMMAND(ID_STL_VIEW_SW, OnViewSW)
	ON_COMMAND(ID_STL_VIEW_SE, OnViewSE)
	ON_COMMAND(ID_STL_VIEW_NW, OnViewNW)
	ON_COMMAND(ID_STL_VIEW_NE, OnViewNE)
	ON_COMMAND(ID_STL_VIEW_SHADE, OnViewShade)
	ON_COMMAND(ID_STL_ZOOM_ALL, OnViewZoomAll)
	ON_COMMAND(ID_STL_ZOOM_IN, OnViewZoomIN)
	ON_COMMAND(ID_STL_ZOOM_OUT, OnViewZoomOUT)

	ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

// CSTLViewerView 构造/析构

CSTLViewerView::CSTLViewerView() noexcept
{
	// TODO: 在此处添加构造代码

}

CSTLViewerView::~CSTLViewerView()
{
}

BOOL CSTLViewerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CGLView::PreCreateWindow(cs);
}

// CSTLViewerView 绘图

// void CSTLViewerView::OnDraw(CDC* /*pDC*/)
// {
// 	CSTLViewerDoc* pDoc = GetDocument();
// 	ASSERT_VALID(pDoc);
// 	if (!pDoc)
// 		return;
// 
// 	// TODO: 在此处为本机数据添加绘制代码
// }


// CSTLViewerView 打印


void CSTLViewerView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

BOOL CSTLViewerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CSTLViewerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CSTLViewerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}

void CSTLViewerView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CSTLViewerView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


void CSTLViewerView::OnViewBack()
{
	OnViewType(stl::eVIEW_BACK);
}

void CSTLViewerView::OnViewBottom()
{
	OnViewType(stl::eVIEW_BOTTOM);
}

void CSTLViewerView::OnViewFront()
{
	OnViewType(stl::eVIEW_FRONT);
}

void CSTLViewerView::OnViewLeft()
{
	OnViewType(stl::eVIEW_LEFT);
}

void CSTLViewerView::OnViewRight()
{
	OnViewType(stl::eVIEW_RIGHT);
}

void CSTLViewerView::OnViewTop()
{
	OnViewType(stl::eVIEW_TOP);
}

void CSTLViewerView::OnViewSW()
{
	OnViewType(stl::eVIEW_SW_ISOMETRIC);
}

void CSTLViewerView::OnViewSE()
{
	OnViewType(stl::eVIEW_SE_ISOMETRIC);
}

void CSTLViewerView::OnViewNE()
{
	OnViewType(stl::eVIEW_NE_ISOMETRIC);
}

void CSTLViewerView::OnViewNW()
{
	OnViewType(stl::eVIEW_NW_ISOMETRIC);
}

void CSTLViewerView::OnViewZoomAll()
{
	ZoomAll();
}

void CSTLViewerView::OnViewZoomIN()
{
	Zoom(0.9);
}

void CSTLViewerView::OnViewZoomOUT()
{
	Zoom(1.1);
}

void CSTLViewerView::OnViewShade()
{
	if (m_pGLDC)
	{
		m_pGLDC->Shading(!m_pGLDC->IsShading());
		Invalidate();
	}
}

void CSTLViewerView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	switch (nChar)
	{
	case VK_UP:
		MoveView(0.0, 0.02);
		break;
	case VK_DOWN:
		MoveView(0.0, -0.02);
		break;
	case VK_RIGHT:
		MoveView(0.02, 0.0);
		break;
	case VK_LEFT:
		MoveView(-0.02, 0.0);
		break;
	default:
		break;
	}

//	CGLView::OnKeyDow(nChar, nRepCnt, nFlags);
}

BOOL CSTLViewerView::GetBox(double& x0, double& y0, double& z0, double& x1, double& y1, double& z1)
{
	CSTLViewerDoc *pDoc = GetDocument();

	if (!pDoc->m_Part.IsEmpty())
	{
		CBox3D box;
		if (pDoc->m_Part.GetBox(box))
		{
			x0 = box.x0;
			y0 = box.y0;
			z0 = box.z0;
			x1 = box.x1;
			y1 = box.y1;
			z1 = box.z1;
			return TRUE;
		}
	}
	return FALSE;
}

// CSTLViewerView 诊断

#ifdef _DEBUG
void CSTLViewerView::AssertValid() const
{
	CGLView::AssertValid();
}

void CSTLViewerView::Dump(CDumpContext& dc) const
{
	CGLView::Dump(dc);
}

CSTLViewerDoc* CSTLViewerView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSTLViewerDoc)));
	return (CSTLViewerDoc*)m_pDocument;
}

void CSTLViewerView::RenderScene(stl::COpenGLDC* pDC)
{
	pDC->DrawCoord();

	CSTLViewerDoc *pDoc = GetDocument();
	if (pDoc != NULL && !pDoc->m_Part.IsEmpty())
	{
		pDoc->m_Part.Draw(pDC);
	}
}

#endif //_DEBUG


// CSTLViewerView 消息处理程序


void CSTLViewerView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CSTLViewerDoc *pDoc = GetDocument();
	if (pDoc != NULL && !pDoc->m_Part.IsEmpty())
	{
		m_pGLDC->BeginSelection(point.x, point.y);
	
		RenderScene(m_pGLDC);
		
		UINT items[64] = { 0 };
		int hits = m_pGLDC->EndSelection(items);

		// 若拾取到实体，则高亮显示。
		if (hits > 0)
		{
			CEntity *pEnt = NULL;
			for (int i = 0; i < hits; ++i)
			{
				pEnt = reinterpret_cast<CEntity*>(items[i]);
				if (pEnt != NULL)
				{
					pEnt->SetHightLight(!pEnt->IsHightLight());
				}				
			}
			Invalidate(FALSE); // 刷新
		}

		CString str;
		str.Format(_T("%d entities selected."), hits);

// 		CMainFrame *pMainFrame = (CMainFrame*)AfxGetMainWnd();
// 		pMainFrame->
		AfxMessageBox(str);
	}

	CGLView::OnLButtonDown(nFlags, point);
}
